@extends('admin.layout.layout', ['row' => @$page])
@php
$admin_id = session('admin.id');
@endphp
@section('contain')



    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions">
                <!--<div class="item">
                    <a href="/admin/download?a=rolls_distribution&b=template" class="btn btn-primary">{{ ts('DOWNLOADDISTTEMPLATE') }}</a>
                </div>
                <div class="item">
                    <a href="/admin/rolls/importDistribution" class="btn btn-primary">{{ ts('BULKDISTIMPORT') }}</a>
                </div>-->
            </div>
        </div>

        <div class="filter-wrapper">
            <form action="/{{ request()->path() }}/" class="form-inline filter-form">
                <div class=" input-group mb-3 custom-control-inline">
                    <input type="text" class="form-control s-input" name="value" value="{{ request()->input('value') }}" placeholder="{{ ts('ROLLSSEARCHTEXT') }}">
                </div>

                <div class="input-group mb-3 custom-control-inline">
                    <div class="input-group-prepend">
                        <span class="input-group-text">{{ @$label ?: ts('PRODUCTTYPE') }}</span>
                    </div>
                    <select name="product_type_id" class="form-control sd_product_filmtype" style="width: 150px" data-value="{{ request()->input('product_type_id') }}">
                        <option value="">{{ ts('select') }}</option>
                        @if(!empty($products))
                        @foreach($products as $v)
                            <option value="{{ $v->id }}" {{ request()->input('product_type_id')==$v->id ? 'selected' : '' }}>{{ ts('product_types.'.$v->id) }}</option>
                        @endforeach
                        @endif
                    </select>
                </div>

                <div class="input-group mb-3 custom-control-inline">
                    <div class="input-group-prepend">
                        <span class="input-group-text">{{ @$label ?: ts('MXH') }}</span>
                    </div>
                    <select name="film_type_id" class="form-control sd-filmtype" style="width: 150px" data-value="{{ request()->input('film_type_id') }}">
                        <option value="">{{ ts('select') }}</option>
                    </select>
                </div>
                <div class="input-group mb-3 custom-control-inline">
                    <select name="status" class="form-control">
                        <option value="">{{ ts('select').' '.ts('status') }}</option>
                        @foreach([0, 2] as $v)
                            <option value="{{ $v }}" {{ (request()->input('status') !== null && request()->input('status') == $v) ? 'selected' : '' }}>{{ ts('RMST'.$v) }}</option>
                        @endforeach

                    </select>
                </div>
                <x-dealer-select></x-dealer-select>

                <x-flatpickr-date-range></x-flatpickr-date-range>

                <div class="mb-3">
                    <button class="btn btn-primary">{{ ts('SEARCH') }}</button>
                    <a href="/{{ request()->path() }}/" class="btn btn-warning">{{ ts('RESET') }}</a>
                </div>
            </form>
        </div>
        <div>
            <div>
                <table class="table text-nowrap bt-table d-none text-center">
                    <thead>
                    <tr class="order_column text-center">
                        <th data-column="id" style="width: 80px">{{ ts('SEQUENCE') }}</th>
                        <th data-column="roll_number" style="width: 150px">{{ ts('rollnumber') }}</th>
                        <th data-column="film_type_id" style="width: 150px">{{ ts('FILMTYPE') }}</th>
                        <th>剩余可分配长度</th>
                        <th style="width: 400px">{{ ts('status') }}</th>
                        <th data-column="date_entered" style="width: 400px">分配时间</th>
                        <th class="fixed-td" style="width:270px">
                            {{ ts('OPERATIONS') }}
                        </th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($data as $k => $v)
                        @php
                            $vRestlen = getRlenV401($v->roll_number, $admin_id) ?: 0;
                        @endphp
                        @if(session('hightlight') && session('hightlight.type')=='rolls' && session('hightlight.id')==$v->id)
                            <tr class="item-list table-warning">
                        @else
                            <tr class="item-list">
                                @endif

                                <td>
                                    <div class="truncated-3">{{ @$data->firstItem()+$k }}</div>
                                </td>

                                <td>
                                    <div class="truncated-3">{{ @$v->roll_number }}</div>
                                </td>
                                
                                <td>
                                    <div class="truncated-3">{{ ts('film_type_master.'.$v->film_type_id) }}</div>
                                </td>

                                <td>
                                    {{ @$vRestlen }}  cm
                                </td>
                                <td>
                                    @if ($vRestlen > 0)
                                        {{ ts('RMST0') }}
                                    @else
                                        {{ ts('RMST2') }}
                                    @endif
                                </td>
                                <td>
                                    {{ fdate2($v->rs_created_at ?: $v->created_at) }}
                                </td>
                                <td  class="fixed-td text-center" style="width:270px">
                                    @if (!empty($menus) && in_array('look', $menus))
                                    <a class="btn btn-success btn-sm" href='/{{ request()->path() }}/{{ $v->id }}'>{{ ts('VIEW') }}</a>
                                    @endif
                                    @if (!empty($menus) && in_array('assign', $menus))
                                    @if( @$vRestlen > 0 )
                                        <button type="button" class="btn btn-primary btn-sm" onclick="openRollsAssignByRollNumber('{{ $v->roll_number }}')">{{ ts('ASSIGN') }}</button>
                                    @else
                                        <button type="button" disabled class="btn btn-secondary btn-sm" onclick="openRollsAssignByRollNumber('{{ $v->roll_number }}')">{{ ts('ASSIGN') }}</button>
                                    @endif
                                    @endif
                                </td>
                            </tr>
                            @endforeach
                    </tbody>
                </table>
            </div>
            <div class="row my-3">
                <div class="col">
                    {{ $data->appends(request()->input())->links() }}
                </div>
            </div>
        </div>

        <form class="modal fade modal-s1" id="rollsAssign">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title font-green">{{ ts('ASSIGNROLL') }}</h5>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                    <div class="modal-body">
                        <div class="form-item">
                            <div class="form-label" required="">{{ ts('rollnumber') }}</div>
                            <div class="form-input">
                                <input type="text" readonly name="roll_id" class="form-control">
                                <div class="text-danger residue-length mt-3">{{ ts('RESIDUELENGTH') }} <b></b> cm</div>
                            </div>
                        </div>

                        <div class="form-item">
                            <div class="form-label" required="">{{ ts('assigntouser') }}</div>
                            <div class="form-input">
                                <select class="form-control" name="user_id" is_show="1" act="sd" >
                                    @foreach(\App\Models\Admin::where('creator_id', $admin_id)->where('status', 1)->get() as $v)
                                        <option value="{{ $v->id }}">{{ $v->abbr ?: $v->company_name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>

                        <div class="form-item">
                            <div class="form-label" required="">{{ ts('length') }}</div>
                            <div class="form-input">
                                <div class="input-group custom-control-inline">
                                    <div class="input-group-prepend">
                                        <label class="input-group-text">
                                            <input type="checkbox" class="icheck_input" name="all_length" value="1">
                                            <span class="ml-1">{{ ts('ALL') }}</span>
                                        </label>
                                    </div>
                                    <input type="number" class="form-control" name="length">
                                    <div class="input-group-append">
                                        <label class="input-group-text">
                                            <span>cm</span>
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="py-3 text-center">
                            <button type="button" class="btn btn-primary" onclick="handleRollsAssign()">{{ ts('assign') }}</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>

    </div>


@endsection


@section('script')
    <script>
        $(function () {
            @if (request()->input('product_type_id'))
                request.get('/admin/rolls/getRollsType', {
                    params: {
                        product_id: '{{ request()->input('product_type_id') }}',
                    }
                })
                .then( function(d) {
                    console.log(d);
                    var data = d.data
                })
                .catch(function (d) {
                    let html = '<option value="">请选择</option>';
                    if (d && d.length > 0) {
                        for (let i in d) {
                            if (d == '' || d[i] == '' || typeof d[i]['id'] == 'undefined') {
                                continue;
                            }
                            html += '<option value="' + d[i]['id'] + '"';
                            @if (request()->input('film_type_id'))
                                if (d[i]['id'] == '{{ request()->input('film_type_id') }}') {
                                    html += ' selected ';
                            }
                            @endif
                            html += '>' + d[i]['value'] + '</option>';
                        }
                    }
                    $(".sd-filmtype").empty().append(html);
                })
            @endif

            $(".sd_product_filmtype").on("change", function () {
               console.log($(this).val());
               let product_id = $(this).val();
               if (product_id == '' || typeof product_id == 'undefined') {
                   $(".sd-filmtype").empty().append('<option value="">请选择</option>');
                   return;
               }
                request.get('/admin/rolls/getRollsType', {
                    params: {
                        product_id: product_id,
                    }
                })
                .then( function(d) {
                    console.log(d);
                    var data = d.data
                    $('.portlet select[name="user_id"]').empty().append(makeSelectOptionsOnly(data))
                })
                .catch(function (d) {
                    let html = '<option value="">请选择</option>';
                    if (d && d.length > 0) {
                        for (let i in d) {
                            if (d == '' || d[i] == '' || typeof d[i]['id'] == 'undefined') {
                                continue;
                            }
                            html += '<option value="' + d[i]['id'] + '">' + d[i]['value'] + '</option>';
                        }
                    }
                    $(".sd-filmtype").empty().append(html);
                })
            });
        });
        $('body').on('change', '[name="sdid"]', function () {
            var val=$(this).val();
            var $that = $(this);
            $('.portlet select[name="user_id"]').empty();
            request.get('/admin/ajax/getUsers', {
                params: {
                    t: 'd',
                    user_id: val
                }
            })
                .then( function(d) {
                    var data = d.data
                    $('.portlet select[name="user_id"]').empty().append(makeSelectOptionsOnly(data))
                })
                .catch(function (d) {
                    toastr.error(d.msg)
                })
        });
        function getUsers() {
            $('.portlet select[name="user_id"]').empty();
            var list=[];
            var show=$('.portlet select[name="user_id"]').attr("is_show");
            if(show==1){
                @foreach(\App\Models\Admin::where('creator_id', $admin_id)->where('status', 1)->get() as $v)
                list.push({
                    label: '{{ addslashes($v->abbr ?: $v->company_name) }}',
                    value: {{ $v->id }}
                });
                @endforeach
            }else{
                @foreach(\App\Models\Admin::where('creator_id', $admin_id)->where('mdtype','<>', 2)->where('status', 1)->get() as $v)
                list.push({
                    label: '{{ addslashes($v->abbr ?: $v->company_name) }}',
                    value: {{ $v->id }}
                });
                @endforeach
            }
            $('.portlet select[name="user_id"]').empty().append(makeSelectOptionsOnly(list))
        }
        $(document).ready(function () {
            if ($('#rollsAssign [name="sdid"]').length >=1 ) {
                request.get('/admin/ajax/getUsers', {
                    params: {
                        t: 'd',
                        user_id: $('[name="sdid"]').val()
                    }
                })
                    .then( function(d) {
                        var data = d.data;
                        $('.portlet select[name="user_id"]').empty().append(makeSelectOptionsOnly(data))
                    })
                    .catch(function (d) {
                        toastr.error(d.msg)
                    })
            }
            $('.table.bt-table').bootstrapTable({
                fixedColumns: true,
                fixedNumber: 1,
                fixedRightNumber: 1,
            }).removeClass('d-none');

            $('#rollsAssign .icheck_input[name="all_length"]').on('ifChecked', function(event){
                var max = $('#rollsAssign').find('.residue-length b').text();
                $('#rollsAssign [name="length"]').val(max);
            });
        })
    </script>
@endsection